package com.qf.item1.controller;

import lombok.extern.slf4j.Slf4j;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author 刘欣
 * @createTime 2023/1/30 16:25
 * @description
 */
@RestController
@Slf4j
public class DownloadController {

    @Value("${upFilePath}")
    private String upFilePath;

    @RequestMapping("/download")
    public void downFile(String filename, HttpServletResponse response) throws IOException {
        log.debug("批量导出文件");
        String finalFilepath = upFilePath + filename;
        // 让浏览器以附件的形式下载文件
        response.setHeader("content-disposition", "attachment;filename=" + filename);
        // 把文件内容写入响应对象的输出流中
        IOUtils.copy(new FileInputStream(finalFilepath), response.getOutputStream());
    }
}
